using System;
using System.Configuration;
using System.Web;
using System.Xml;
using umlungu.engineX.blogX;
using umlungu.engineX.FtpX;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (Pty) ltd
	Author:		Alan Benington	
	Started:	2005-05-15	
	Status:		release	
	Version:	2.0.0
	Buidl:		20070616
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/
	
/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20070524:	Modification for .Net 2.0
	20070616:	Addition of config object
	-----------------------------------------------------------------------	*/
	
	namespace umlungu.engineX.webX.thoughts.Admin.service {
	/// <summary>
	/// umlungu engineX web framework, handlerX class.
	/// </summary>
	public class SoapX : umlungu.engineX.webX.handlerX {

		private const bool isDebug = false;	// need to set dynamically from config

		#region Invisible properties
		private string remote_host, remote_path, remote_user, remote_password, remote_docpath;
		private string subdir, subsubdir;
		#endregion

		#region Constants
		private const string CONFIG_ROOT = "WebX";
		private const string CONFIG_ELEM = "FtpX";
		#endregion

		#region Visible properties
		public string SubDir {	get	{ return subdir; }	set	{ subdir = value; }	}
		public string SubSubDir { get { return subsubdir; } set { subsubdir = value; } }
		#endregion

		#region Constructors/Destructors
		/// <summary>Constructor</summary>
		public SoapX(string serviceName, string methodName) : base(serviceName, methodName, isDebug, "admin") {
			x_config config = new x_config();
			remote_host = config.Value(String.Concat(CONFIG_ROOT, "/", CONFIG_ELEM, "/@host"));;
			remote_user = config.Value(String.Concat(CONFIG_ROOT, "/", CONFIG_ELEM, "/@username"));;
			remote_password = config.Value(String.Concat(CONFIG_ROOT, "/", CONFIG_ELEM, "/@password"));;
			remote_path = config.Value(String.Concat(CONFIG_ROOT, "/", CONFIG_ELEM, "/@path"));;
			remote_docpath = config.Value(String.Concat(CONFIG_ROOT, "/", CONFIG_ELEM, "/@docpath"));;
		}
		#endregion

		#region Public methods
		#endregion
		
		#region Protected methods
		/// <summary>
		/// Method _ProcessImage:: gets images from form and ftp's them to server
		/// </summary>
		protected void _ProcessImage(XmlDocument xmldom, profileX profile, contentX content) {
			_Debug("_ProcessImage", "file count:", Request.Files.Count.ToString());
			try {
				for (int i=0; i < Request.Files.Count; i++) {
					HttpPostedFile thisfile = Request.Files.Get(i);
					int last = thisfile.FileName.LastIndexOf("\\");
					string filename = Server.MapPath(String.Concat("images", thisfile.FileName.Substring(last)));
					_Debug("_ProcessImage", "File::name:", filename, ":last:", last.ToString());
					thisfile.SaveAs(filename);
					
					ftpImage(filename, profile);
				}
			} catch(System.Exception e) {
				_Debug("_ProcessImage", "Error::", e.Message, ":trace:", e.StackTrace);
			}
		}

		/// <summary>
		/// Method _ProcessBlogImage:: gets images from form, generates a new name and ftp's them to server
		/// </summary>
		protected void _ProcessBlogImage(XmlDocument xmldom, profileX profile, contentX content) {
			_Debug("_ProcessBlogImage", "file count:", Request.Files.Count.ToString());
			try {
				for (int i = 0; i < Request.Files.Count; i++) {
					HttpPostedFile thisfile = Request.Files.Get(i);
					int last = thisfile.FileName.LastIndexOf(".");
					//Guid newimg = Guid.NewGuid();
					//string thisname = newimg.ToString();
					string thisname = String.Concat("image", profile.GetProfileValue("id"), "_", profile.GetProfileValue("iid"));
					string thisext = thisfile.FileName.Substring(last);
					string filename = Server.MapPath(String.Concat("images\\", thisname, thisext));
					_Debug("_ProcessBlogImage", "File::name:", filename, ":last:", last.ToString());
					thisfile.SaveAs(filename);

					ftpImage(filename, profile);
				}
			} catch (System.Exception e) {
				_Debug("_ProcessBlogImage", "Error::", e.Message, ":trace:", e.StackTrace);
			}
		}

		/// <summary>
		/// Method _ProcessDocument:: gets document from form and ftp's them to server
		/// </summary>
		protected void _ProcessDocument(XmlDocument xmldom, profileX profile, contentX content) {
			_Debug("_ProcessDocument", "file count:", Request.Files.Count.ToString());
			try {
				for (int i=0; i < Request.Files.Count; i++) {
					HttpPostedFile thisfile = Request.Files.Get(i);
					int last = thisfile.FileName.LastIndexOf("\\");
					string filename = Server.MapPath(String.Concat("uploads", thisfile.FileName.Substring(last)));
					_Debug("_ProcessDocument", "File::name:", filename, ":last:", last.ToString());
					thisfile.SaveAs(filename);
					
					ftpDocument(filename, profile);
				}
			} catch(System.Exception e) {
				_Debug("_ProcessDocument", "Error::", e.Message, ":trace:", e.StackTrace);
			}
		}
		
		/// <summary>
		/// Method _CallSoap:: calls delegate(s)
		/// </summary>
		protected override string _Soap(string opName) {
			string thisurl = (string) Application["AdminWebServiceX"];
			_Debug("_Soap", "opName:", opName, " Url:", thisurl);
			AdminWSProxy adminx = new AdminWSProxy();
			adminx.Url = thisurl;
			return(SoapCall(new SoapProxyCall(adminx.Call)));
		}
		#endregion
		
		#region Private methods
		/// <summary>
		/// Method ftpImage:: does the actual ftp'ing of image
		/// </summary>
		private void ftpImage(string imgfile, profileX profile) {
			_Debug("ftpImage", "imgfile:", imgfile);
			string remotePath = String.Concat(remote_path, subdir, (subsubdir == null) ? "" : "/", subsubdir);
			_Debug("ftpImage", "remotePath:", remotePath);
			x_ftp ff = new x_ftp(remote_host, remotePath, remote_user, remote_password, 21, true);
			try {
				ff.login();
				ff.chdir(remotePath);
				ff.upload(imgfile);
			} catch(System.Exception e) {
				_Debug("ftpImage", "Error::", e.Message, ":trace:", e.StackTrace);
			}
			if (ff != null)
				ff.close();
		}
		
		/// <summary>
		/// Method ftpDocument:: does the actual ftp'ing of document
		/// </summary>
		private void ftpDocument(string docfile, profileX profile) {
			_Debug("ftpDocument", "docfile:", docfile);
			string remotePath = String.Concat(remote_docpath, subdir);
			_Debug("ftpDocument", "remotePath:", remotePath);
			x_ftp ff = new x_ftp(remote_host, remotePath, remote_user, remote_password, 21, true);
			try {
				ff.login();
				ff.chdir(remotePath);
				ff.upload(docfile);
			} catch(System.Exception e) {
				_Debug("ftpDocument", "Error::", e.Message, ":trace:", e.StackTrace);
			}
			if (ff != null)
				ff.close();
		}
		#endregion
	}
}
		
